package cn.com.surker.lab.service.impl;

import cn.com.surker.common.MessageCenterException;
import cn.com.surker.lab.dao.LabLevelOptimizeDao;
import cn.com.surker.lab.entity.LabLevelOptimize;
import cn.com.surker.lab.entity.LabLevelOptimizeMaterial;
import cn.com.surker.lab.dao.LabLevelOptimizeMaterialDao;
import cn.com.surker.lab.service.LabLevelOptimizeMaterialService;
import cn.com.surker.spring.RestResultDto;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 强度等级优化材料消耗详细表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-09-23
 */
@Service
public class LabLevelOptimizeMaterialServiceImpl extends ServiceImpl<LabLevelOptimizeMaterialDao, LabLevelOptimizeMaterial> implements LabLevelOptimizeMaterialService {

	@Resource
	private LabLevelOptimizeMaterialDao labLevelOptimizeMaterialDao;
	@Resource
	private LabLevelOptimizeDao labLevelOptimizeDao;


	/**
	 * 查询施工配比材料详情
	 * @param levelConstructId
	 * @return
	 */
	public List<LabLevelOptimizeMaterial> findByConstruct(Long levelConstructId) {
		if (StringUtils.isNull(levelConstructId))
			return null;
		QueryWrapper<LabLevelOptimizeMaterial> query = new QueryWrapper<>();
		query.eq("status", 0);
		query.eq("level_optimize_id", levelConstructId);
		query.orderByAsc("id");
		List<LabLevelOptimizeMaterial> constructMaterialList = labLevelOptimizeMaterialDao.selectList(query);
		return constructMaterialList;
	}

	/**
	 * 保存施工配比材料信息
	 * @param materialList
	 * @return
	 */
	public RestResultDto saveBatchConstructMaterial(LabLevelOptimize levelConstruct, List<LabLevelOptimizeMaterial> materialList) {
		if (materialList == null || materialList.size() == 0)
			throw new MessageCenterException("参数错误");
		if (levelConstruct == null || StringUtils.isNull(levelConstruct.getId()))
			throw new MessageCenterException("标准配合比信息错误");
		List<LabLevelOptimizeMaterial> tempList = new ArrayList<>();
		Date nowDate = new Date();
		Double sumUsed = 0.0;
		for (int i = 0; i < materialList.size(); i++) {
			LabLevelOptimizeMaterial material = materialList.get(i);
			if (StringUtils.isNull(material.getMaterialId()))
				throw new MessageCenterException((i + 1) + "子项材料信息为空");
			material.setLevelOptimizeId(levelConstruct.getId());
			sumUsed += material.getUsed();
			if (StringUtils.isNull(material.getId()) || material.getId().intValue() < 1) {
				material.setCreateTime(nowDate);
				material.setCreator(levelConstruct.getCreator());
			} else {
				material.setUpdateTime(nowDate);
				material.setUpdator(levelConstruct.getCreator());
			}
			tempList.add(material);
		}
		if (tempList == null || tempList.size() == 0)
			throw new MessageCenterException("参数错误");
		Integer code = labLevelOptimizeMaterialDao.saveBatch(tempList);
		if (code > 0) {
			levelConstruct.setVolumeWeight(StringUtils.formatDouble(sumUsed, 1));
			labLevelOptimizeDao.updateById(levelConstruct);
			return RestResultDto.success("保存成功");
		}
		throw new MessageCenterException("没有更新的数据");
	}

	/**
	 * 保存施工配比材料信息
	 * @param constructMaterial
	 * @return
	 */
	public RestResultDto saveConstructMaterial(LabLevelOptimizeMaterial constructMaterial) {
		if (constructMaterial == null || StringUtils.isNull(constructMaterial.getLevelOptimizeId()))
			throw new MessageCenterException("参数错误");
		Date nowDate = new Date();
		constructMaterial.setCreateTime(nowDate);
		Integer code = labLevelOptimizeMaterialDao.insert(constructMaterial);
		if (code > 0) {
			Double sumUsed = labLevelOptimizeMaterialDao.totalSumUsed(constructMaterial.getLevelOptimizeId());
			UpdateWrapper<LabLevelOptimize> updateWrapper = new UpdateWrapper<>();
			updateWrapper.set("updator", StringUtils.convertString(constructMaterial.getUpdator()));
			updateWrapper.set("update_time", nowDate);
			updateWrapper.set("volume_weight", sumUsed);
			updateWrapper.eq("id", constructMaterial.getLevelOptimizeId());
			code = labLevelOptimizeDao.update(new LabLevelOptimize(), updateWrapper);
			if (code > 0)
				return RestResultDto.success("保存成功");
		}
		throw new MessageCenterException("保存失败");
	}

	/**
	 * 修改施工配比材料信息
	 * @param constructMaterial
	 * @return
	 */
	public RestResultDto updateConstructMaterial(LabLevelOptimizeMaterial constructMaterial) {
		if (constructMaterial == null || StringUtils.isNull(constructMaterial.getId()))
			throw new MessageCenterException("参数错误");
		LabLevelOptimizeMaterial material = labLevelOptimizeMaterialDao.selectById(constructMaterial.getId());
		if (material == null || material.getStatus().intValue() == 1)
			throw new MessageCenterException("业务数据不存在或已被禁用");
		Date nowDate = new Date();
		constructMaterial.setUpdateTime(nowDate);
		Integer code = labLevelOptimizeMaterialDao.updateById(constructMaterial);
		if (code > 0) {
			Double sumUsed = labLevelOptimizeMaterialDao.totalSumUsed(constructMaterial.getLevelOptimizeId());
			UpdateWrapper<LabLevelOptimize> updateWrapper = new UpdateWrapper<>();
			updateWrapper.set("updator", StringUtils.convertString(constructMaterial.getUpdator()));
			updateWrapper.set("update_time", nowDate);
			updateWrapper.set("volume_weight", sumUsed);
			updateWrapper.eq("id", constructMaterial.getLevelOptimizeId());
			code = labLevelOptimizeDao.update(new LabLevelOptimize(), updateWrapper);
			if (code > 0)
				return RestResultDto.success("更新成功");
		}

		throw new MessageCenterException("更新失败");
	}

	/**
	 * 删除配合比材料信息
	 * @param id
	 * @param status
	 * @param remark
	 * @param creator
	 * @return
	 */
	public RestResultDto updateStatus(Long id, Integer status, String remark, String creator) {
		if (StringUtils.isAllParamNull(id, status))
			throw new MessageCenterException("参数错误");
		LabLevelOptimizeMaterial material = labLevelOptimizeMaterialDao.selectById(id);
		if (material == null)
			throw new MessageCenterException("业务数据不存在");
		Date nowDate = new Date();
		material.setUpdateTime(new Date());
		material.setUpdator(creator);
		material.setStatus(status);
		if (!StringUtils.isAllParamNull(remark))
			material.setRemark(remark);
		Integer code = labLevelOptimizeMaterialDao.updateById(material);
		if (code > 0) {
			Double sumUsed = labLevelOptimizeMaterialDao.totalSumUsed(material.getLevelOptimizeId());
			UpdateWrapper<LabLevelOptimize> updateWrapper = new UpdateWrapper<>();
			updateWrapper.set("updator", creator);
			updateWrapper.set("update_time", nowDate);
			updateWrapper.set("volume_weight", sumUsed);
			updateWrapper.eq("id", material.getLevelOptimizeId());
			code = labLevelOptimizeDao.update(new LabLevelOptimize(), updateWrapper);
			if (code > 0)
				return RestResultDto.success("更新成功");
		}
		throw new MessageCenterException("更新失败");
	}
	
}